package com.jhx.video.activity

import android.os.Bundle
import com.jhx.common.cache.SimpleCache
import com.jhx.common.framework.QuickBaseActivity
import com.jhx.common.model.Event
import com.jhx.common.model.Video
import com.jhx.video.R
import com.jhx.video.widget.ResolutionDialog
import com.tencent.ugc.TXRecordCommon
import kotlinx.android.synthetic.main.activity_record_setting.*
import org.greenrobot.eventbus.EventBus

/**
 * 视频设置页面
 *
 * @author Jack 2018-03-10 16:08
 */
class VideoSettingActivity : QuickBaseActivity() {
    // 是否添加水印
    private var isAdd: Boolean = true
    private var mDialog: ResolutionDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_record_setting)
        setTitle("视频设置")
        initListener()
        setVideoText()
    }

    private fun initListener() {
        txt_sharpness.setOnClickListener {
            if(mDialog == null) {
                mDialog = ResolutionDialog(this)
            }
            mDialog?.onItemClick = { _, index ->
                when(index) {
                    0 -> {
                        SimpleCache.put(Video.VIDEO_RESOLUTION, TXRecordCommon.VIDEO_QUALITY_HIGH)
                        SimpleCache.put(Video.VIDEO_RATE, 1200)
                        txt_sharpness.text = "超清"
                    }
                    1 -> {
                        SimpleCache.put(Video.VIDEO_RESOLUTION, TXRecordCommon.VIDEO_QUALITY_MEDIUM)
                        SimpleCache.put(Video.VIDEO_RATE, 2400)
                        txt_sharpness.text = "高清"
                    }
                    2 -> {
                        SimpleCache.put(Video.VIDEO_RESOLUTION, TXRecordCommon.VIDEO_QUALITY_LOW)
                        SimpleCache.put(Video.VIDEO_RATE, 3600)
                        txt_sharpness.text = "标清"
                    }
                }
                val event = Event()
                event.code = Video.CODE_RESOLUTION
                EventBus.getDefault().post(event)
            }
            if(mDialog?.isShowing == false) {
                mDialog?.show()
            }
        }

        txt_watermark_status.setOnClickListener {
            isAdd = !isAdd
            val drawable = if (isAdd) {
                resources.getDrawable(R.mipmap.icon_open)
            } else {
                resources.getDrawable(R.mipmap.icon_close)
            }
            SimpleCache.put(Video.KEY_ADD_WATERMARK, isAdd)
            drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
            txt_watermark_status.setCompoundDrawables(null, null, drawable, null)
        }
    }

    private fun setVideoText() {
        val resolution = SimpleCache.get<Int>(Video.VIDEO_RESOLUTION)
        val add = SimpleCache.get<Boolean>(Video.KEY_ADD_WATERMARK)
        val drawable = if (add != null && add == false) {
            resources.getDrawable(R.mipmap.icon_close)
        } else {
            resources.getDrawable(R.mipmap.icon_open)
        }
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        txt_watermark_status.setCompoundDrawables(null, null, drawable, null)
        when(resolution) {
            TXRecordCommon.VIDEO_QUALITY_HIGH -> txt_sharpness.text = "超清"
            TXRecordCommon.VIDEO_QUALITY_MEDIUM ->  txt_sharpness.text = "高清"
            TXRecordCommon.VIDEO_QUALITY_LOW -> txt_sharpness.text = "标清"
        }
    }
}